﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Cellula.Model;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Support;
using Cn.Edwon.Web;
using Cn.Cellula.Model.Content;
namespace Cn.Cellula.Website.Admin.Content.Vote {
    public partial class EditVote : PageSupport {
        IVoteBiz biz = BizFactory.Get<IVoteBiz>();
        String VoteKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            VoteKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                if (!String.IsNullOrEmpty(VoteKey)) {
                    FillForm(VoteKey);
                }
            }
        }

        /// <summary>
        /// 表单初始化数据
        /// </summary>
        /// <param name="typeKey"></param>
        private void FillForm(String rVoteKey) {
            try {
                TVoteEO eo = biz.Load<TVoteEO>(rVoteKey);
                this.txt_Desc.Text = eo.VoteDesc;
                this.txt_EndDate.Text = (eo.EndDate == null) ? "" : Convert.ToDateTime(eo.EndDate).ToString("yyyy-MM-dd HH:mm:ss");
                this.txt_StartDate.Text = (eo.StartDate == null) ? "" : Convert.ToDateTime(eo.StartDate).ToString("yyyy-MM-dd HH:mm:ss");
                this.txt_VoteTitle.Text = eo.VoteTitle;
                this.ddl_Filter.SelectedValue = eo.IsIpFilter + "";
                this.rbl_Select.SelectedValue = eo.IsMultiSelect + "";
            } catch { }
        }

        private void BindEOData(ref TVoteEO eo) {
            eo.VoteTitle = this.txt_VoteTitle.Text;
            eo.VoteDesc = this.txt_Desc.Text;
            try {
                eo.StartDate = Convert.ToDateTime(this.txt_StartDate.Text);
            } catch {
                eo.StartDate = null;
            }
            try {
                eo.EndDate = Convert.ToDateTime(this.txt_EndDate.Text);
            } catch {
                eo.EndDate = null;
            }
            try {
                eo.IsIpFilter = Convert.ToInt32(this.ddl_Filter.SelectedValue);
            } catch {
                eo.IsIpFilter = 1;
            }
            try {
                eo.IsMultiSelect = Convert.ToInt32(this.rbl_Select.SelectedValue);
            } catch {
                eo.IsMultiSelect = 0;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e) {
            //---- 增加新项 ---------------------------------
            if (String.IsNullOrEmpty(VoteKey)) {
                TVoteEO eo = new TVoteEO();
                eo.OwnerKey = this.OwnerKey;
                BindEOData(ref eo);
                biz.Save<TVoteEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("投票活动", eo.VoteTitle, this.Actor, Request);
                SiteInfo.SendSucceed(Page, "List.aspx?t=" + eo.VoteKey);
            } else {//更新----------------------------------
                TVoteEO eo = biz.Load<TVoteEO>(VoteKey);
                BindEOData(ref eo);
                biz.Update<TVoteEO>(eo);
                BizFactory.Get<ILogBiz>().LogUpdate("投票活动", eo.VoteTitle, this.Actor, Request);
                SiteInfo.SendSucceed(Page, "List.aspx?t=" + eo.VoteKey);
            }
        }
    }
}
